using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Salient.ServiceModel
{
    /// <summary>
    /// Working on reworking some found code that presents what seems to be a good idea
    /// but horrible implementation.... lol.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ServiceHoster<T> : IDisposable
    {
        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion
    }
}

namespace Salient.ServiceModel
{
    /// <summary>
    /// Existing horrible implementation
    /// </summary>
    internal class DebugServiceHost : IDisposable
    {
        private static readonly bool isEnabled;
        public static string Address = @"http://localhost:9999/ExecutionService/ServiceExecute/";
        public static Binding Binding = new WSHttpBinding();

        private static string iServiceExecuteTypeName =
            "ServiceSide.ExecutionService.IServiceExecute, ServiceSide.ExecutionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

        private static string serviceExecuteTypeName =
            "ServiceSide.ExecutionService.ServiceExecute, ServiceSide.ExecutionService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

        private readonly ServiceHost debugHost;
        private bool disposed;

        static DebugServiceHost()
        {
            isEnabled = Type.GetType(serviceExecuteTypeName) != null;
        }

        public DebugServiceHost()
        {
            Type serviceType = Type.GetType(serviceExecuteTypeName);
            Type iServiceType = Type.GetType(iServiceExecuteTypeName);
            debugHost = new ServiceHost(serviceType);

            debugHost.AddServiceEndpoint(iServiceType, Binding, Address);
            debugHost.Open();
        }

        public static bool IsEnabled
        {
            get { return isEnabled; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (disposed)
            {
                return;
            }
            debugHost.Close();
            GC.SuppressFinalize(this);
        }

        #endregion
    }

    //public static class Provider<T> where T : class
    //{
    //    private static DebugServiceHost dbh;
    //    public static void CheckAndRunDebugService()
    //    {
    //        if (DebugServiceHost.IsEnabled)
    //        {
    //            dbh = new DebugServiceHost();
    //        }
    //    }
    //    public static string RemoteURL { get; set; }
    //    private static T iServiceExecute = null;
    //    private static T GetInterface()
    //    {
    //        if (iServiceExecute != null)
    //            return iServiceExecute;
    //        iServiceExecute = CreateClient();
    //        return iServiceExecute;
    //    }
    //    private static T CreateClient()
    //    {
    //        string address = "";
    //        if (DebugServiceHost.IsEnabled)
    //        {
    //            address = DebugServiceHost.Address;
    //        }
    //        else
    //        {
    //            address = String.Format("{0}/Service.svc", Provider.RemoteURL);
    //        }
    //        return new ServiceExecuteReference.ServiceExecuteClient("DefaultEndPoint", address);
    //    }
    //}
}